package web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import web.bean.XiaoQu;
import web.dao.Impl.XiaoQuDaoImpl;
import web.dao.XiaoQuDao;
import web.service.Impl.XiaoQuServiceImpl;
import web.service.XiaoQuService;
import web.utils.R;
import web.vo.PageVo;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

public class XiaoQuController {
    public static final String FILE_PATH="E:\\Tools\\idea\\peixun\\wuye\\src\\web\\upload";
    XiaoQuDao xiaoQuDao=new XiaoQuDaoImpl();
    XiaoQuService xiaoQuService=new XiaoQuServiceImpl();
    public R page(JSONObject jsonObject){
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer pageNo=jsonObject.getInteger("pageNo");
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");
        PageVo<XiaoQu> byInfo = xiaoQuService.findByInfo(pageSize, pageNo, startTime, endTime, name);
        System.out.println(R.ok().setData(byInfo));
        return R.ok().setData(byInfo);
    }
    public R add(JSONObject jsonObject){
        Integer userId = jsonObject.getInteger("userId");
        String xiaoqumingcheng = jsonObject.getString("xiaoqumingcheng");
        String address = jsonObject.getString("address");
        String area = jsonObject.getString("area");
        Integer dongshu = jsonObject.getInteger("dongshu");
        Integer zonghushu = jsonObject.getInteger("zonghushu");
        String lvhualv = jsonObject.getString("lvhualv");
        String icon = jsonObject.getString("icon");
        String kaifashangmingcheng = jsonObject.getString("kaifashangmingcheng");
        String wuyegongsimingcheng = jsonObject.getString("wuyegongsimingcheng");
        String description = jsonObject.getString("description");
        Integer state = jsonObject.getInteger("state");
        XiaoQu xiaoQu=new XiaoQu();
        xiaoQu.setUserId(userId);
        xiaoQu.setXiaoqumingcheng(xiaoqumingcheng);
        xiaoQu.setAddress(address);
        xiaoQu.setArea(area);
        xiaoQu.setDongshu(dongshu);
        xiaoQu.setZonghushu(zonghushu);
        xiaoQu.setLvhualv(lvhualv);
        xiaoQu.setIcon(icon);
        xiaoQu.setKaifashangmingcheng(kaifashangmingcheng);
        xiaoQu.setWuyegongsimingcheng(wuyegongsimingcheng);
        xiaoQu.setDescription(description);
        xiaoQu.setStatus(state);
        //调用service，录入一个新的小区
        int result = xiaoQuService.add(xiaoQu);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }
    }
    public R del(JSONObject jsonObject){
        JSONArray delid = jsonObject.getJSONArray("delid");
        Integer[] deleteId = new Integer[delid.size()];
        for (int i = 0; i < delid.size(); i++) {
            deleteId[i]=delid.getInteger(i);
        }
        int result = xiaoQuService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }
    public void showImage(HttpServletRequest request, HttpServletResponse response){
        String fileName = request.getParameter("fileName");
        File file=new File(FILE_PATH+File.separator+fileName);
        try {
            BufferedImage read = ImageIO.read(file);
            ImageIO.write(read,"jpg",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public R edit(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        XiaoQu xiaoQu = JSONObject.parseObject(s, XiaoQu.class);
        int i = xiaoQuService.editInfo(xiaoQu);
        if(i>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }
    public R showXiaoQu(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        XiaoQu xiaoqu = xiaoQuService.findXiaoQuById(id);
        return R.ok().setData(xiaoqu);
    }
    public R findNameAll(Map<String,String[]> map){
        List<XiaoQu> xiaoQus=xiaoQuService.findNameAll();
        return R.ok().setData(xiaoQus);
    }
    public XiaoQu findNameById(Integer id){
        return xiaoQuService.findNameById(id);
    }
}
